﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Avarton_Scanner.Logging;
using Avarton_Scanner.Connection;

namespace Avarton_Scanner
{
    /// <summary>
    /// Interaction logic for History.xaml
    /// </summary>
    public partial class History : Window
    {
        Logger log = new Logger();
        DatabaseClass db = new DatabaseClass();
        String applicationName = "History";

        public History()
        {
            InitializeComponent();
        }

        private void btnClose_MouseDown(object sender, MouseButtonEventArgs e)
        {
            WindowCollection screens = Application.Current.Windows;
            foreach (Window x in screens)
            {
                if (x.Title.ToString().Equals("History"))
                {
                    x.Close();
                }
            }
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();
            }
            catch (Exception)
            {
                //do nothing
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //finally get all the already known networks and clients from the database.
            try
            {
                db.MakeConnection();
                cboNetworks.Items.Add("All");
                List<String> names = db.GetNetworkNames();
                foreach (String name in names)
                {
                    cboNetworks.Items.Add(name);
                }
                cboNetworks.SelectedValue = "All";
                //initial fill up of the list
                lstClients.ItemsSource = db.GetClients();
                lblStatus.Content = "Done loading all data from database.";
            }
            catch (Exception ex)
            {
                MessageBox.Show("The application could not load the database information. This will disable the abbility to load and save all the known data.");
                btnFilter.IsEnabled = false;
                lblStatus.Content = "Error.";
                log.WriteLine(applicationName, ex.Message);
            }
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            String selectedNetworkName = cboNetworks.SelectedValue.ToString();
            if (selectedNetworkName == "All")
            {
                lstClients.ItemsSource = db.GetClients();
                lblStatus.Content = "Done getting: All.";
            }
            else
            {
                lstClients.ItemsSource = db.GetClientsWhere("WHERE SSID LIKE '" + selectedNetworkName + "'");
                lblStatus.Content = "Done getting: "+ selectedNetworkName +".";
            }
        }
    }
}
